library(tidyverse)
library(magrittr)
library(data.table)
library(lubridate)
library(hms)
library(ggthemes)
theme_set(theme_few())

### SET WORKING DIRECTORY HERE ###
path_to_archive <- "replication/"
data_dir <- paste0(path_to_archive, "data/")
setwd(data_dir)
plot_dir <- paste0(path_to_archive, "plots/")
tables_dir <- paste0(path_to_archive, "tables/")

dates <- seq(mdy("09/01/2012"), mdy("11/06/2012"), by='days')


g_count <- function(d) {
	cat(as.character(d),"\n")
	readRDS(paste0(data_dir, "dd/tc_groups_", as.character(d), ".rds")) %>%
		mutate(n_t = map_dbl(t_c, ~ .[group=="T", .N]),
			   n_c1 = map_dbl(t_c, ~ .[group=="C1", .N]),
			   n_c2 = map_dbl(t_c, ~ .[group=="C2", .N]),
			   n_c12 = map_dbl(t_c, ~ .[group=="C1+C2", .N])) %>%
		filter(n_t > 0, (n_c1 + n_c2 + n_c12 > 0)) %>% 
		unnest %>% 
		as.data.table %>%
		.[group!="T",group:="C"] %>%
		.[,.(t_count=sum(group=="T"), c_count = sum(group=="C")),by=.(device_id)]
	}

total_counts <- map(dates, g_count) %>% 
	rbindlist %>% 
	.[,map(.SD, sum), by=.(device_id)]

sd(total_counts$t_count)
# [1] 69.65572
mean(total_counts$t_count)
# [1] 29.37413
median(total_counts$t_count)
# [1] 4

### FIGURE B.3
ggplot(aes(x=c_count, y=t_count), data=total_counts) +
	geom_point(alpha=0.02) +
	xlim(0,100) + 
	ylim(0,100) +
	labs(x="Number of appearances of device in control group", y = "Number of appearances of device in treatment group")


ggsave(paste0(plot_dir, "device_t_c_count.png"))